package cn.acac.yygh.hosp.service.impl;

import cn.acac.yygh.exception.MyException;
import cn.acac.yygh.hosp.config.DepartmentRepository;
import cn.acac.yygh.hosp.service.DepartmentService;
import cn.acac.yygh.model.hosp.Department;
import cn.acac.yygh.vo.hosp.DepartmentQueryVo;
import cn.acac.yygh.vo.hosp.DepartmentVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 存储科室的信息
     * @param map
     */
    @Override
    public void saveDepartment(Map<String, Object> map) {
        // 将map对象转换为pojo对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);
        // 根据医院编号 和 科室编号查询
        Department department1 = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        // 判断department1是否存在
        if(department1 == null){
            // 进行添加操作
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            // 进行修改操作
            department.setId(department1.getId());
            department.setCreateTime(department1.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(department1.getIsDeleted());
            departmentRepository.save(department);
        }
    }

    /**
     * 返回第三方医院科室的分页信息
     * @param map
     * @return
     */
    @Override
    public Page<Department> selectPage(Map<String, Object> map) {
        // 获取分页信息
        String hoscode = (String) map.get("hoscode");  // 获取医院编号
        Integer pageNum = Integer.parseInt((String) map.get("page"));   // 当前页
        Integer pageSize = Integer.parseInt((String) map.get("limit"));     // 每页数量
        // 条件
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        // 创建分页对象
        Pageable pageable = PageRequest.of(pageNum - 1,pageSize);
        Page<Department> pageInfo = departmentRepository.findAll(example, pageable);
        return pageInfo;
    }

    /**
     * 根据医院编号和科室编号删除科室信息
     * @return
     */
    @Override
    public void removeDepartment(Map<String, Object> switchMap) {
        // 获取医院编号和科室编号
        String hoscode = (String) switchMap.get("hoscode");
        String depcode = (String) switchMap.get("depcode");
        // 先根据医院和科室编号查询MongoDB数据库
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department == null){
            throw new MyException(50000,"科室信息不存在，请检查后在删除！");
        }
        departmentRepository.deleteById(department.getId());
    }

    /**
     * 根据医院的编号查询医院底下所有的科室信息
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        // 设置查询条件
        Department departmentQ = new Department();
        departmentQ.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQ);
        // 查询科室信息
        List<Department> all = departmentRepository.findAll(example);
        // 封装大科室的集合
        List<DepartmentVo> departmentVoList = new ArrayList<>();
        // 获取所有的科室信息后，根据大科室的编号进行分组
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // 遍历map集合
        Set<Map.Entry<String, List<Department>>> entries = collect.entrySet();
        for (Map.Entry<String, List<Department>> entry : entries) {
            // 大科室编号
            String bigCode = entry.getKey();
            // 大科室中的小科室
            List<Department> childDepartment = entry.getValue();
            // 封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);   // 设置大科室编号
            departmentVo.setDepname(childDepartment.get(0).getBigname()); // 设置大科室名称

            // 封装小科室
            List<DepartmentVo> childDept = new ArrayList<>();
            for (Department department : childDepartment) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepname(department.getDepname());
                departmentVo1.setDepcode(department.getDepcode());
                childDept.add(departmentVo1);
            }
            // 将小科室封装到大科室的子属性中
            departmentVo.setChildren(childDept);
            departmentVoList.add(departmentVo);
        }
        return departmentVoList;
    }

    /**
     * 根据医院编号和科室编号查询科室名称
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String findDeptNameByHoscodeAngDeptcode(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    /**
     * 根据医院编号和科室编号获取科室
     * @param hospcode
     * @param deptcode
     * @return
     */
    @Override
    public Department getDepartment(String hospcode, String deptcode) {
        return departmentRepository.findByHoscodeAndDepcode(hospcode.toString(),deptcode.toString());
    }

}
